package com.wyh.contorller;

import com.wyh.entity.Announcements;
import com.wyh.entity.RestBean;
import com.wyh.mapper.AnnouncementsMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Validated
@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {

    @Resource
    AnnouncementsMapper mapper;

    //通过id查询公告
    @PostMapping("/find")
    public RestBean<List<Announcements>> selectAnnouncementsById(@RequestParam("id") int id) {
        List<Announcements> announcements = mapper.selectAnnouncementsById(id);
        if (announcements != null && !announcements.isEmpty()) {
            return RestBean.success(announcements);
        }
        return RestBean.failure(404);
    }

    //添加公告
    @PostMapping("/add")
    public RestBean<String> add(Announcements announcements) {
        try {
            Announcements A = new Announcements();
            A.setName(StringUtils.hasText(announcements.getName()) ? announcements.getName() : null);
            A.setInformation(StringUtils.hasText(announcements.getInformation()) ? announcements.getInformation() : null);
            A.setType(StringUtils.hasText(announcements.getType()) ? announcements.getType() : null);
            A.setTime(StringUtils.hasText(announcements.getTime()) ? announcements.getTime() : null);
            A.setRemark(StringUtils.hasText(announcements.getRemark()) ? announcements.getRemark() : null);
            mapper.addAnnouncements(announcements);
            return RestBean.success("添加成功");
        } catch (Exception e) {
            return RestBean.failure(404, "error" + e.getMessage());
        }
    }

    //获取所有公告
    @GetMapping("/getAll")
    public RestBean<List<Announcements>> getAllAnnouncements() {
        try {
            List<Announcements> announcements = mapper.getAll();
            if (announcements == null) {
                return RestBean.success(null);
            } else {
                return RestBean.success(announcements);
            }
        } catch (Exception e) {
            return RestBean.failure(500);
        }
    }

    //修改公告
    @PutMapping("/update")
    public RestBean<String> update(@RequestBody Announcements announcements) {
        try {
            Announcements existingAnnouncements = mapper.findExist(Long.parseLong(String.valueOf(announcements.getId())));
            if (existingAnnouncements != null) {

                announcements.setType(StringUtils.hasText(announcements.getType()) ? announcements.getType() : existingAnnouncements.getType());
                announcements.setInformation(StringUtils.hasText(announcements.getInformation()) ? announcements.getInformation() : existingAnnouncements.getInformation());
                announcements.setTime(StringUtils.hasText(announcements.getTime()) ? announcements.getTime() : existingAnnouncements.getTime());
                announcements.setRemark(StringUtils.hasText(announcements.getRemark()) ? announcements.getRemark() : existingAnnouncements.getRemark());
                mapper.updateAnnouncements(announcements);
                return RestBean.success("修改成功");
            } else {
                return RestBean.failure(404, "公告不存在");
            }
        } catch (Exception e) {
            return RestBean.failure(404, "error" + e.getMessage());
        }
    }


    //删除公告
    @DeleteMapping("/delete")
    public RestBean<String> delete(long id) {
        Announcements existingAnnouncements = mapper.findExist(id);
        if (existingAnnouncements != null) {
            try {
                mapper.deleteAnnouncements(id);
                return RestBean.success("删除成功");
            } catch (Exception e) {
                return RestBean.failure(404, "error" + e.getMessage());
            }
        }
        return RestBean.failure(404, "公告不存在");
    }

}
